//
//  KLNewShouruViewController.m
//  KunLun
//
//  Created by Mac on 2018/3/23.
//  Copyright © 2018年 stoneobs@icloud.com. All rights reserved.
//

#import "KLNewShouruViewController.h"
#import "KLSouruModel.h"
#import "KLFecthMoneyViewController.h"
@interface KLNewShouruViewController ()
@property(nonatomic, strong) STSegementButton                     *segementButton;
@property(nonatomic, strong) NSArray                     *titlesArray;
@property(nonatomic, strong) KLSouruModel                     *model;
@property(nonatomic, strong) NSArray                     *dataSouce;
@end

@implementation KLNewShouruViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的收入";
    [self st_setRightItemWithTitle:@"提现" titleColor:[UIColor whiteColor]];
    if (!self.user) {
        [KLUtils fetch_MineInfoWithHandle:^(KLUserModel *userModel, STError *error) {
            self.user = userModel;
            [self configSubView];
        }];
    }else{
        [self configSubView];
    }
   
    // Do any additional setup after loading the view.
}
- (void)configDataSouce{
    NSMutableArray * sectionOne = [NSMutableArray new];
    if (self.user.shops_id.integerValue !=0 ) {
        STTableEasyModel * model =  [[STTableEasyModel alloc] initWithTextString:@"当前商户余额" detailString:self.model.balance.description];
        [sectionOne addObject:model];
    }
    if (self.user.channel_status.integerValue == 3) {
        STTableEasyModel * model = [[STTableEasyModel alloc] initWithTextString:@"当前渠道商总额"
                                                                   detailString:self.model.total.description];
        [sectionOne addObject:model];
    }
    NSArray * sectionTwo = @[
                             [[STTableEasyModel alloc] initWithTextString:@"今日匀加速市场订单数"
                                                             detailString:self.model.to_yun.description],
                             [[STTableEasyModel alloc] initWithTextString:@"今日民车出租订单数"
                                                             detailString:self.model.to_mc.description],

                             [[STTableEasyModel alloc] initWithTextString:@"今日宾馆住宿订单数"
                                                             detailString:self.model.to_bg.description],
//                             [[STTableEasyModel alloc] initWithTextString:@"今日饭店订桌订单数"
//                                                             detailString:self.model.to_mc.description],
                             [[STTableEasyModel alloc] initWithTextString:@"今日地方特产订单数"
                                                             detailString:self.model.to_df.description],
                             ];
    
    NSArray * sectionThress = @[
                             [[STTableEasyModel alloc] initWithTextString:@"累计匀加速市场订单数"
                                                             detailString:self.model.lei_yun.description],
                             [[STTableEasyModel alloc] initWithTextString:@"累计民车出租订单数"
                                                             detailString:self.model.lei_mc.description],

                             [[STTableEasyModel alloc] initWithTextString:@"累计宾馆住宿订单数"
                                                             detailString:self.model.lei_bg.description],
//                             [[STTableEasyModel alloc] initWithTextString:@"累计饭店订桌订单数"
//                                                             detailString:self.model.lei_df.description],
                             [[STTableEasyModel alloc] initWithTextString:@"累计地方特产订单数"
                                                             detailString:self.model.lei_df.description],
                             ];
    self.dataSouce = @[sectionOne,sectionTwo,sectionThress];
    [self.tableView reloadData];
}
- (void)st_rightBarAction:(id)sender{
    KLFecthMoneyViewController * vc = [KLFecthMoneyViewController new];
    vc.money = self.user.balance;
    vc.qumoney = self.model.total;
    vc.titles = self.titlesArray;
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark --subView
- (void)configSubView{
    NSMutableArray * titles = [NSMutableArray new];
    if (self.user.shops_id.integerValue !=0 ) {
        [titles addObject:@"商家收入"];
    }
    if (self.user.channel_status.integerValue == 3) {
        [titles addObject:@"渠道商收入"];
    }
    self.titlesArray = [titles copy];
    __weak typeof(self) weakSelf =  self;
    self.segementButton = [[STSegementButton alloc] initWithFrame:CGRectMake(0, 64, UIScreenWidth, 44) andTitle:self.titlesArray handle:^(STSegementButton *sender) {
        [weakSelf.tableView.mj_header beginRefreshing];
    }];
    self.segementButton.backgroundColor = [UIColor whiteColor];
    self.segementButton.lineSelectedColor = KL_BlueBackGroundColor;
    self.segementButton.butTitleSelectedColor = KL_BlueBackGroundColor;
    self.segementButton.butTitleColor = KL_secendTextColor;
    self.segementButton.lineColor = [UIColor whiteColor];
    self.segementButton.cureentIndex = 0;
    self.segementButton.autoMoveWithClic = YES;
    self.segementButton.cureentIndex = 0;
    [self.view addSubview:self.segementButton];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    if (IOS11 && [self.tableView respondsToSelector:@selector(setContentInsetAdjustmentBehavior:)]) {
        [self.tableView setContentInsetAdjustmentBehavior:@(2)];
    }
    self.tableView.frame = CGRectMake(0, [KLUtils navgationBarBootom] + 44, UIScreenWidth, [KLUtils tabBarTop] + 49 -[KLUtils navgationBarBootom] - 44);
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self sendMoneyAndOrderRequest];
    }];
    [self.tableView.mj_header beginRefreshing];
    
}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.dataSouce.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return [self.dataSouce[section] count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 44;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 24;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuseIdentifier];
        
    }
    STTableEasyModel * model = self.dataSouce[indexPath.section][indexPath.row];
    [cell setSt_tableEasyModel:model];
    cell.accessoryType = UITableViewCellAccessoryNone;
    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}

#pragma mark --NetWork Method
- (void)sendMoneyAndOrderRequest{
    NSString * title = self.titlesArray[self.segementButton.cureentIndex];
    NSString * url ;
    if ([title isEqualToString:@"渠道商收入"]) {
        url = [NSString stringWithFormat:@"%@%@%@",serviceUrl,@"/api/user/qu_money?token=",[STUserManger defult].token];
    }
    if ([title isEqualToString:@"商家收入"]) {
        url = [NSString stringWithFormat:@"%@%@%@",serviceUrl,@"/api/user/sh_money?token=",[STUserManger defult].token];
    }
    NSMutableDictionary * dic = [NSMutableDictionary new];
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [self.tableView.mj_header endRefreshing];
                                                 [SVProgressHUD dismiss];
                                                 NSDictionary * dataDic = responseObject[@"data"];
                                                 NSLog(@"json = %@",dataDic.mj_JSONString);
                                                 self.model = [KLSouruModel mj_objectWithKeyValues:dataDic];
                                                 [self configDataSouce];
                                                 
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [self.tableView.mj_header endRefreshing];
                                                 [SVProgressHUD dismiss];
                                                 //[SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
}

@end
